package com.example.controller;

import com.example.model.Department;
import com.example.model.Employee;
import com.example.model.Gender;
import com.example.model.User;
import com.example.model.dlo.LoginUser;
import com.example.model.dlo.UpdateUser;
import com.example.service.DepartmentService;
import com.example.service.EmployeeService;
import com.example.service.UserService;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;

import java.beans.PropertyEditorSupport;

@Controller
public class CompanyController {
    @Autowired
    private DepartmentService departmentService;
    @Autowired
    private UserService userService; // 用户服务（需自行实现）
    @Autowired
    private EmployeeService employeeService;
    private LoginUser loginUser;



    @GetMapping (value = {"/login"})
    public String login() {
        return "blog/login";
    }



    @GetMapping (value = {"/index"})
    public String index(HttpServletRequest request,Model model) {
        loginUser = (LoginUser) request.getSession().getAttribute("loginUser");
        model.addAttribute("loginUser", loginUser);
        return "blog/index";
    }

    @GetMapping("/register")
    public String register() {
        return "blog/register"; // 返回注册页面的视图名称
    }

    @GetMapping(value = {"/personalCenter"})
    public String personalCenter(HttpServletRequest request,Model model) {
        loginUser = (LoginUser) request.getSession().getAttribute("loginUser");
        String departmentName = departmentService.getNameById(loginUser.getEmployee().getDepartmentId());

        model.addAttribute("user", loginUser);
        model.addAttribute("departmentName", departmentName);
        System.out.println("Session loginUser: " + loginUser);
        return "blog/personalCenter";}

    @PostMapping("/personalCenter1")
    public String updatePersonalInfo(@ModelAttribute("user") UpdateUser updateUser,HttpServletRequest request, Model model) {
        // 处理更新逻辑，例如：
        // employeeService.update(employeeDTO);
        loginUser = (LoginUser) request.getSession().getAttribute("loginUser");
        Employee employee = loginUser.getEmployee();
        User user=new User();
        employee.setName(updateUser.getUsername());
        employee.setGender(updateUser.getGender());
        employee.setBirthDate(updateUser.getBirthDate());
        employee.setEmail(updateUser.getEmail());
        employee.setPhone(updateUser.getPhone());

        System.out.println(employee.getGender());
        user.setId(loginUser.getId());
        user.setUsername(updateUser.getUsername());
        user.setPassword(loginUser.getPassword());
        user.setEmployeeId(loginUser.getEmployee().getId());
        user.setRoleId(loginUser.getRole().getId());
        user.setStatus(loginUser.getStatus());
        user.setLastLogin(loginUser.getLastLogin());
        user.setUpdateTime(loginUser.getUpdateTime());
        user.setCreateTime(loginUser.getCreateTime());
        user.setEmail(updateUser.getEmail());
        employeeService.update(employee);
        userService.UpdateUser(user);
        model.addAttribute("loginUser", loginUser);
        return "redirect:/index";
    }


    @GetMapping(value = {"/tokenChange"})
    public String tokenChange() {return "blog/tokenChange";}

    @GetMapping(value = {"/businessManagement"})
    public String business() {return "blog/businessManagement";}

    @GetMapping(value={"/businessEdit"})
    public String businessEdit() {return "blog/businessEdit";}

    @GetMapping(value = {"/departmentManagement"})
    public String department() {return "blog/departmentManagement";}

    @GetMapping(value={"/departmentEdit"})
    public String departmentEdit() {return "blog/departmentEdit";}


}